package com.wwz.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwz.entity.*;
import com.wwz.exception.EcoBootException;
import com.wwz.mapper.PermissionMapper;
import com.wwz.model.PermissionModel;
import com.wwz.query.PermissionQuery;
import com.wwz.service.IPermissionMenuService;
import com.wwz.service.IPermissionPointService;
import com.wwz.service.IPermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;

/**
 * (Permission)表服务实现类
 *
 * @author makejava
 * @since 2022-09-25 10:56:18
 */
@Service
public class IPermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Resource
    private IPermissionMenuService permissionMenuService;

    @Resource
    private IPermissionPointService permissionPointService;

    @Resource
    private IPermissionApiServiceImpl permissionApiService;

    @Override
    public IPage<Permission> toPage(PermissionQuery query) {
        return this.page(query.toPage(), Wrappers.<Permission>lambdaQuery()
                .like(StrUtil.isNotEmpty(query.getEnVisible()), Permission::getEnVisible, query.getEnVisible())
                .like(StrUtil.isNotEmpty(query.getName()), Permission::getName,query.getName())
                .orderByDesc(Permission::getCreateTime)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpData(PermissionModel permissionModel) {
        Permission permission = permissionModel.getPermission();
        PermissionMenu permissionMenu = permissionModel.getPermissionMenu();
        PermissionPoint permissionPoint = permissionModel.getPermissionPoint();
        PermissionApi permissionApi = permissionModel.getPermissionApi();
        // 权限添加
        Permission one = getById(permission.getId());
        Integer type = permission.getType();
        if (one == null){
            String uuid = StrUtil.uuid();
            permission.setId(uuid);
            // 公司id
            permission.setPid("7ac32217-25bf-459a-b07d-b739c5c33210");
            permission.setCreateTime(new Date());
            switch (type){
                case 1:
                    permissionMenu.setId(uuid);
                    permissionMenuService.save(permissionMenu);
                    break;
                case 2:
                    permissionPoint.setId(uuid);
                    permissionPointService.save(permissionPoint);
                    break;
                case 3:
                    permissionApi.setId(uuid);
                    permissionApiService.save(permissionApi);
                    break;
                default: throw new EcoBootException("类型有误");
            }
            save(permission);
            return;
        }
        // 修改
        upDataPerm(permissionMenu, permissionPoint, permissionApi, type);
        updateById(permission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id) {
        Permission permission = this.getById(id);
        PermissionMenu permissionMenu = permissionMenuService.getById(id);
        PermissionPoint permissionPoint = permissionPointService.getById(id);
        PermissionApi permissionApi = permissionApiService.getById(id);

        if (permission != null){
            removeById(id);
        }
        if (permissionMenu != null){
            permissionMenuService.removeById(id);
        }
        if (permissionPoint != null){
            permissionPointService.removeById(id);
        }
        if (permissionApi != null){
            permissionApiService.removeById(id);
        }
    }


    /**
     * 修改
     * @param permissionMenu
     * @param permissionPoint
     * @param permissionApi
     * @param type
     */
    private void upDataPerm(PermissionMenu permissionMenu,
                           PermissionPoint permissionPoint,
                           PermissionApi permissionApi,
                           Integer type)
    {
        switch (type){
            case 1:
                permissionMenuService.updateById(permissionMenu);
                break;
            case 2:
                permissionPointService.updateById(permissionPoint);
                break;
            case 3:
                permissionApiService.updateById(permissionApi);
                break;
            default: throw new EcoBootException("类型有误");
        }
    }
}

